﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Modules
{
    public partial class DieuChinhKho : DevExpress.XtraEditors.XtraForm
    {
        String sErr = "";
        DataRow _row = null;
        String IDKH = "";
        int idThem = 87;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnThem.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
                //btnSua.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
                //btnXoa.Visible = frmMain.IsPermis(idXoa, main.CurrentPermis.PERMISSION);
                btnLuu.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
            }
        }
        public DieuChinhKho()
        {
            InitializeComponent();
        }

        private void panelControl4_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void RefresForm(string p)
        {

            txtSLTon.EditValue = p;
            txtSoTien.EditValue = p;
            txtSoTienSau.EditValue = p;
            txtSLTonSau.EditValue = p;

        }

        private void EnabledForm(bool p)
        {



            txtSoTienSau.Properties.ReadOnly = !p;
            txtSLTonSau.Properties.ReadOnly = !p;

            if (frmMain.__khoaSo)
            {
                btnThem.Enabled = false;
            }
            else
            {
                btnThem.Enabled = !p;
            }

            btnLuu.Enabled = p;
            btnBoQua.Enabled = p;

            btnDong.Enabled = !p;


        }


        private void DieuChinhKho_Load(object sender, EventArgs e)
        {
            LoadHangHoa();
            RefresForm("");
            EnabledForm(false);
            SetPermission();
        }

        private void tlHangHoa_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (e.Node != null)
            {
                BUS.LOAIHANGControl ctr = new BUS.LOAIHANGControl();
                gridSanPham.DataSource = ctr.GetTonKho(e.Node.GetValue("ParentID").ToString(), e.Node.GetValue("ID").ToString(), 0, null, ref sErr);
            }
        }
        protected void LoadHangHoa()
        {
            BUS.NHOMHANGControl ctr = new BUS.NHOMHANGControl();
            tlHangHoa.DataSource = ctr.GetTreeList("", ref sErr);
            tlHangHoa.ExpandAll();
        }

        private void gvSanPham_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                //if (btnThem.Enabled == false)
                //{
                _row = ((DataRowView)gvSanPham.GetRow(e.RowHandle)).Row;

                //txtMaHang.Text = _row["MAHANG"].ToString();
                //txtTenHang.Text = _row["TENHANG"].ToString();
                //txtQuyCach.Text = _row["QUYCACH"].ToString();
                //txtDonViTinh.Text = _row["DVTINH"].ToString();
                BUS.LOAIHANGControl lhctr = new BUS.LOAIHANGControl();
                BUS.TONKHOControl tonkhoctr = new BUS.TONKHOControl();
                DTO.LOAIHANGInfo lhinf = lhctr.Get(_row["MAHANG"].ToString(), ref sErr);
                DTO.TONKHOInfo tkinf = new DTO.TONKHOInfo();
                tkinf = tonkhoctr.Get(lhinf.NHOMHANG, lhinf.MAHANG, ref sErr);
                txtSLTon.EditValue = tkinf.SOLUONG;
                txtSoTien.EditValue = tkinf.SOTIEN;

                //txtSLTonSau.EditValue = 0;
                //txtSoTienSau.EditValue = 0;

                gbSLTonKho.Text = "Số liệu tồn kho :" + _row["TENHANG"].ToString();

                //}
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            EnabledForm(true);
            //   RefresForm("");


            btnThem.Enabled = false;
            txtSLTonSau.Focus();

        }
        protected String SaveHeader(ref DTO.GIAODICHInfo inf)
        {
            BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
            //DataTable dt = ctr.GetALLByCT(null, null, frmMain.__ngayMoSo, ref sErr);
            //int i = 1;
            //string ct = (dt.Rows.Count + i).ToString("00000");

            //while (ctr.IsExist(ct, null, frmMain.__ngayMoSo))
            //{
            //    ct = (dt.Rows.Count + i).ToString("00000");
            //    i++;
            //}
            BUS.CommonControl cmm = new BUS.CommonControl();
            String ct = cmm.GetSoCTNextNo(frmMain.__ngayMoSo.ToString("yyyy-MM-dd"));

            inf.SOCT = ct;

            inf.NGAYCT = frmMain.__ngayMoSo;
            inf.MAKH = "";
            //    inf.TIENHANG = Convert.ToDouble(txtTienHang.EditValue);
            //     inf.PHIVC = Convert.ToDouble(txtPhiVanChuyen.EditValue);
            //     inf.CHIETKHAU = Convert.ToDouble(txtChieutkhau.EditValue);
            if (Convert.ToDouble(txtSoTienSau.EditValue) > Convert.ToDouble(txtSoTien.EditValue) || Convert.ToDouble(txtSLTonSau.EditValue) > Convert.ToDouble(txtSLTon.EditValue))
            {
                inf.LOAICT = "DKNH001";
                inf.TONGTIEN = Convert.ToDouble(txtSoTienSau.EditValue) - Convert.ToDouble(txtSoTien.EditValue);
            }
            else { inf.LOAICT = "DK001"; inf.TONGTIEN = Convert.ToDouble(txtSoTien.EditValue) - Convert.ToDouble(txtSoTienSau.EditValue); }



            inf.CONGNO = inf.TONGTIEN;
            inf.TIENMAT = inf.TONGTIEN;
            inf.LOAITIEN = "VND";
            inf.DIACHIKH = "";
            inf.DIENTHOAI = "";

            inf.NOIDUNG = "Điều chỉnh kho";
            inf.TINHTRANG = "Normal";
            inf.INHD = true;
            inf.CREATEBY = frmMain.__userID;
            inf.MODIFIEDBY = frmMain.__userID;
            inf.CREATEDATE = frmMain.__ngayMoSo;
            inf.MODIFIEDDATE = frmMain.__ngayMoSo;

            ctr.Add(inf, ref sErr);

            return sErr;
        }


        protected String SaveDetail(DTO.GIAODICHInfo gdinf)
        {
            BUS.LOAIHANGControl lhctr = new BUS.LOAIHANGControl();
            BUS.TONKHOControl tonkhoctr = new BUS.TONKHOControl();
            BUS.NXHANGControl ctr = new BUS.NXHANGControl();
            DTO.LOAIHANGInfo lhinf = lhctr.Get(_row["MAHANG"].ToString(), ref sErr);

            int line = 1;
            double soluong = 0;
            bool xuatkho = false;
            if (gdinf.LOAICT == "DKNH001") { xuatkho = false; soluong = Convert.ToDouble(txtSLTonSau.EditValue) - Convert.ToDouble(txtSLTon.EditValue); }
            else { xuatkho = true; soluong = Convert.ToDouble(txtSLTon.EditValue) - Convert.ToDouble(txtSLTonSau.EditValue); }
            double giavon = soluong == 0 ? 0 : gdinf.TONGTIEN / soluong;
            DTO.NXHANGInfo inf = new DTO.NXHANGInfo() { SOCT = gdinf.SOCT, LINE = line, LOAICT = gdinf.LOAICT, NGAYCT = gdinf.NGAYCT, LOAIHANG = lhinf.MAHANG, LOCATION = "0001", QUYCACH = lhinf.QUYCACH, DVTINH = lhinf.DVTINH, SOLUONG = soluong, GIAVON = giavon, SOTIEN = gdinf.TONGTIEN, TINHTRANG = gdinf.TINHTRANG, XUATKHO = xuatkho, GIABAN = 0 };

            ctr.Add(inf, ref sErr);
            if (sErr == "")
            {
                DTO.TONKHOInfo tkinf = new DTO.TONKHOInfo();
                tkinf = tonkhoctr.Get(lhinf.NHOMHANG, lhinf.MAHANG, ref sErr);
                if (inf.XUATKHO)
                { tkinf.SOLUONG = tkinf.SOLUONG - inf.SOLUONG; tkinf.SOTIEN = tkinf.SOTIEN - inf.SOTIEN; }
                else { tkinf.SOLUONG = tkinf.SOLUONG + inf.SOLUONG; tkinf.SOTIEN = tkinf.SOTIEN + inf.SOTIEN; }

                tkinf.GIAVON = tkinf.SOTIEN / tkinf.SOLUONG;

                tonkhoctr.Update(tkinf);
            }



            return sErr;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            DTO.GIAODICHInfo gdInf = new DTO.GIAODICHInfo();
            sErr = SaveHeader(ref gdInf);

            if (sErr == "")
            {
                sErr = SaveDetail(gdInf);
            }

            //BUS.LOAIHANGControl lhctr = new BUS.LOAIHANGControl();
            //BUS.TONKHOControl tonkhoctr = new BUS.TONKHOControl();
            //DTO.LOAIHANGInfo lhinf = lhctr.Get(_row["MAHANG"].ToString(), ref sErr);
            //DTO.TONKHOInfo tkinf = new DTO.TONKHOInfo();
            //tkinf = tonkhoctr.Get(lhinf.NHOMHANG, lhinf.MAHANG, ref sErr);
            //tkinf.SOTIEN = Convert.ToDouble(txtSoTienSau.EditValue);
            //tkinf.SOLUONG = Convert.ToDouble(txtSLTonSau.EditValue);
            //tkinf.GIAVON = tkinf.SOLUONG == 0 ? 0 : tkinf.SOTIEN / tkinf.SOLUONG;

            //sErr = tonkhoctr.Update(tkinf);

            if (sErr != "")
                XtraMessageBox.Show(sErr);
            else
            {
                BUS.GIAODICHControl gdctr = new BUS.GIAODICHControl();
                DTO.GIAODICHInfo gdinf = new DTO.GIAODICHInfo();

                //DataTable dt = gdctr.GetALLByCT(null, null, frmMain.__ngayMoSo, ref sErr);
                //int i = 1;
                //String soct = (dt.Rows.Count + i).ToString("00000");

                //while (gdctr.IsExist(soct, null, frmMain.__ngayMoSo))
                //{
                //    soct = (dt.Rows.Count + i).ToString("00000");
                //    i++;
                //}
                BUS.CommonControl cmm = new BUS.CommonControl();
                String soct = cmm.GetSoCTNextNo(frmMain.__ngayMoSo.ToString("yyyy-MM-dd"));

                gdinf.SOCT = soct;
                if (Convert.ToDouble(txtSLTonSau.EditValue) > Convert.ToDouble(txtSLTon.EditValue))
                    gdinf.LOAICT = "DK001";
                else
                    gdinf.LOAICT = "DKNH001";
                gdinf.NGAYCT = frmMain.__ngayMoSo;
                


                XtraMessageBox.Show("Lưu thành công");
                RefresForm("");
                EnabledForm(false);
            }

        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            RefresForm("");
            EnabledForm(false);
        }

        private void txtSLTonSau_EditValueChanged(object sender, EventArgs e)
        {
            if (txtSLTonSau.Text != "" && txtSLTon.Text != "0")
            {
                txtSoTienSau.EditValue = Convert.ToDouble(txtSoTien.EditValue) / Convert.ToDouble(txtSLTon.EditValue) * Convert.ToDouble(txtSLTonSau.EditValue);

            }
        }

        private void DieuChinhKho_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);

            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);

            }
        }
    }
}